import { observable, action ,toJS} from "mobx";


import { Modal } from "antd";
const confirm = Modal.confirm;

export default class UserAdmin{
  constructor(root){
    this.root = root;
  }
  
  // 刷新列表
  @observable loading=false;
  // 分页数据
  @observable paging={}
  
  // 当前状态  insert update
  @observable status="paging";
  
  // 查询数据
  @observable where={};
  // 用户表单数据
  @observable form = {};

  // 初始化
  @action init=()=>{
    this._init();
  }
  
  @action set=(k,v)=>{
    const arr = k.split("-");
    const loop = (obj,arr,v)=>{
      const temp = arr.splice(0,1);
      if(arr.length){
        if(!obj[temp]){obj[temp]={};}
        obj = obj[temp];
        loop(obj,arr,v);
      }else{
        obj[temp] = v;
      }
    };
    loop(this,arr,v);
    switch(k){
      case "form-loading":
        this.root.set("loading",v);
        break;
      case "loading":
      default:
        this.root.set(k,v);
    }
  }
  
  // input赋值
  @action change=(e,field)=>{
    switch(field){
    case "form-data-visible":
      this.set(field,e?1:0);
      break;
    case "form-data-authority":
      this.set(field,e);
      break;
    default:
      this.set(field,e.target.value);
    }
    this.set("form-tip","");
  }

  // 显示所有
  @action all=()=>{
    this._init();
  }
  // 搜索
  @action find=()=>{
    this._paging(1);
  }
  
  // 添加编辑用户按钮事件
  @action modal=(form,record)=>{
    this._init_form();
    this.set("form-loading",false);
    this.set("form-condition",form);
    console.log(toJS(record));
    if(form === "update"){
      this.set("form-data-id",record.id);
      this.set("form-data-uname",record.uname);
      this.set("form-data-authority",record.authority);
      this.set("form-data-authoritylist",JSON.parse(record.authoritylist));
    }
  }
  // 删除用户按钮事件
  @action delete=record=>{
    confirm({
      title: "删除确认",
      content: "是否真的删除 ".concat(record.uname,"?"),
      okText:"确认",
      cancelText:"取消",
      onOk:()=>{
        this.set("loading",true);
        this.root.ajax.post({
          url:this.root.url,
          data:{act:"admin-admin-delete",id:record.id},
          success:json=>{
            this.set("loading",false);
            if(json.status !== "ok"){this.root.alert(json);return;}
            this._paging(this.paging.nowPage);
          }
        });
      }
    });
  };

  // 提交insert/update表单数据
  @action submit=()=>{
    if(!this.form.data.uname){
      this.root.alert({status:"error",msg:"请输入用户名"});
      return;
    }else if(this.form.condition==="insert" && !this.form.data.pwd){
      this.root.alert({status:"error",msg:"请输入密码"});
      return;
    }
    this.set("loading",true);
    this.set("form-loading",true);
 
    this.root.ajax.post({
      url:this.root.url,
      data:{
        act:"admin-admin-".concat(this.form.condition),
        uname:this.form.data.uname,
        pwd:this.form.data.pwd,
        id:this.form.data.id,
        authority:this.form.data.authority,
        authoritylist:JSON.stringify(this.form.data.authoritylist),
      },
      success:json=>{
        this.set("loading",false);
        this.set("form-loading",false);
        this.root.alert(json);
        if(json.status === "ok"){
          if(this.form.condition==="update"){
            this.set("form-condition","paging");
          }
          this._init_form();
          this._paging(this.paging.nowPage);
        }
      }
    });
  };
  
  // 重置
  @action resetForm=()=>{
    this._init_form();
  };
 
  
  @action page=p=>this._paging(p);
  // 用户列表分页
  _paging(p){
    this.set("loading",true);
    p = p?p:1;
    this.root.ajax.post({
      url:this.root.url,
      data:{act:"admin-admin-paging",p:p,field:this.where.field, value:this.where.value, condition:this.where.condition},
      success:json=>{
        this.set("loading",false);
        if(json.status !== "ok"){this.root.alert(json);return;}
        this.set("paging",json.data);
      }
    });
  }
  // 初始化
  _init(){
    this._init_form();
    this._init_where();
    this._init_paging();
    this._paging(1);
  }
  // 用户表单初始化
  _init_form(){
    this.set("form",{
      condition:"paging", // insert|update
      title:"",
      tip:"",
      loading:false,
      data:{
        id:0,
        authority:"0",
        authoritylist:[],
        uname:"",
        pwd:""
      }// 表单内数据-id,phone,visible,authority,uname,pwd
    });
  }
  // 查询表单初始化
  _init_where(){
    this.set("where",{
      field:"uname",
      value:"",
      condition:"="
    });
  }



  // 分页数据初始化
  _init_paging(){
    this.paging={
      data:[],
      maxPage:1,
      page :1,
      maxNum:1,
      num:10,
    };
  }

}